﻿#include "Dx2TextureManager.h"



void Dx2TextureManager::LoadTexture(
    ID3D12Device* device,
    ID3D12GraphicsCommandList* cmdList)
{
    auto grassTex = std::make_unique<Texture>();
    grassTex->Name = "grassTex";
    grassTex->Filename =  L"../../Res/Textures/grass.dds";
    ThrowIfFailed(DirectX::CreateDDSTextureFromFile12(device,cmdList,
        grassTex->Filename.c_str(),grassTex->Resource,grassTex->UploadHeap));
    
    auto waterTex = std::make_unique<Texture>();
    waterTex->Filename = L"../../Res/Textures/water1.dds";
    waterTex->Name = "waterTex";
    ThrowIfFailed(DirectX::CreateDDSTextureFromFile12(device,cmdList,
        waterTex->Filename.c_str(),waterTex->Resource,waterTex->UploadHeap));

    auto fenceTex = std::make_unique<Texture>();
    fenceTex->Filename = L"../../Res/Textures/WireFence.dds";
    fenceTex->Name = "fenceTex";
    ThrowIfFailed(DirectX::CreateDDSTextureFromFile12(device,cmdList,
        fenceTex->Filename.c_str(),fenceTex->Resource,fenceTex->UploadHeap));

    auto iceTex = std::make_unique<Texture>();
    iceTex->Name = "iceTex";
    iceTex->Filename = L"../../Res/Textures/ice.dds";
    ThrowIfFailed(DirectX::CreateDDSTextureFromFile12(device,cmdList,
        iceTex->Filename.c_str(),iceTex->Resource,iceTex->UploadHeap));

    auto white1x1Tex = std::make_unique<Texture>();
    white1x1Tex->Name = "white1x1Tex";
    white1x1Tex->Filename = L"../../Res/Textures/white1x1.dds";
    ThrowIfFailed(DirectX::CreateDDSTextureFromFile12(device,cmdList,
        white1x1Tex->Filename.c_str(),white1x1Tex->Resource,white1x1Tex->UploadHeap));

    auto treeTex = std::make_unique<Texture>();
    treeTex->Name = "treeArrayTex";
    treeTex->Filename = L"../../Res/Textures/treeArray2.dds";
    ThrowIfFailed(DirectX::CreateDDSTextureFromFile12(device,cmdList,
        treeTex->Filename.c_str(),treeTex->Resource,treeTex->UploadHeap));

    mTextures[grassTex->Name] = std::move(grassTex);
    mTextures[waterTex->Name] = std::move(waterTex);
    mTextures[fenceTex->Name] = std::move(fenceTex);
    mTextures[white1x1Tex->Name] = std::move(white1x1Tex);
    mTextures[iceTex->Name] = std::move(iceTex);
    mTextures[treeTex->Name] = std::move(treeTex);
}

void Dx2TextureManager::BuildDescriptorHeap(
    ID3D12Device* device,
    CD3DX12_CPU_DESCRIPTOR_HANDLE hDescriptor,
    UINT cbvSrvDescriptorSize)
{
    auto grassTex = mTextures["grassTex"]->Resource;
    auto waterTex = mTextures["waterTex"]->Resource;
    auto fenceTex = mTextures["fenceTex"]->Resource;
    auto iceTex = mTextures["iceTex"]->Resource;
    auto white1x1Tex = mTextures["white1x1Tex"]->Resource;
    auto treeTex = mTextures["treeArrayTex"]->Resource;
	
    D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
    srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
    srvDesc.Format = grassTex->GetDesc().Format;
    srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
    srvDesc.Texture2D.MostDetailedMip = 0;
    srvDesc.Texture2D.MipLevels = -1;
    device->CreateShaderResourceView(grassTex.Get(),&srvDesc,hDescriptor);

    hDescriptor.Offset(1,cbvSrvDescriptorSize);
    srvDesc.Format = waterTex->GetDesc().Format;
    device->CreateShaderResourceView(waterTex.Get(),&srvDesc,hDescriptor);

    hDescriptor.Offset(1,cbvSrvDescriptorSize);
    srvDesc.Format = fenceTex->GetDesc().Format;
    device->CreateShaderResourceView(fenceTex.Get(),&srvDesc,hDescriptor);

    hDescriptor.Offset(1,cbvSrvDescriptorSize);
    srvDesc.Format = iceTex->GetDesc().Format;
    device->CreateShaderResourceView(iceTex.Get(),&srvDesc,hDescriptor);

    hDescriptor.Offset(1,cbvSrvDescriptorSize);
    srvDesc.Format = white1x1Tex->GetDesc().Format;
    device->CreateShaderResourceView(white1x1Tex.Get(),&srvDesc,hDescriptor);

    hDescriptor.Offset(1,cbvSrvDescriptorSize);
    srvDesc.Format = treeTex->GetDesc().Format;
    device->CreateShaderResourceView(treeTex.Get(),&srvDesc,hDescriptor);

    mDescriptorSize = 6;
}

